﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using School.Entities;
using System.Collections;
using System.Drawing;

namespace School.Pages.BOOKS
{
    public partial class SRV_Books_Issue : System.Web.UI.Page
    {
        private SchoolEntities db = new SchoolEntities();
        private IEnumerable<object> lst = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1104) == true)
                {
                    if (!this.Page.IsPostBack)
                    {
                        lblName.Visible = false;
                        DropDownList1.Visible = false;
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridBind();
        }
      
        private void GridBind()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtNum.Text))
                {
                    lst = School.Service.BOOKS.SRV_BOOK_ISSUE.VoucherSearch(int.Parse(txtNum.Text));
                    if (lst != null)
                    {
                        gvdata.DataSource = lst;
                        gvdata.DataBind();

                    }
                }
                else if (!string.IsNullOrEmpty(datepicker13.Text) && !string.IsNullOrEmpty(txtNum.Text))
                {
                    CultureInfo SaCulture = new CultureInfo("ar-eg");
                    DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
                    hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
                    DateTime datestart = Convert.ToDateTime(datepicker13.Text, hijriDateInfo);

                    lst = School.Service.BOOKS.SRV_BOOK_ISSUE.VoucherSearch(int.Parse(txtNum.Text), datestart);
                    if (lst != null)
                    {
                        gvdata.DataSource = lst;
                        gvdata.DataBind();
                    }
                }

                else if (!string.IsNullOrEmpty(datepicker13.Text) && !string.IsNullOrEmpty(TextBox1.Text))
                {
                    CultureInfo SaCulture = new CultureInfo("ar-eg");
                    DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
                    hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
                    DateTime datestart = Convert.ToDateTime(datepicker13.Text, hijriDateInfo);
                    DateTime dateEnd = Convert.ToDateTime(TextBox1.Text, hijriDateInfo);
                    IEnumerable<ACC_Voucher> lstVouch = db.ACC_Voucher.Where(s => s.Date >= datestart && s.Date <= dateEnd && s.Expense_Id == 4).ToList();
                    if (lstVouch != null)
                    {
                        DropDownList1.DataSource = FillFullName(lstVouch);
                        DropDownList1.DataBind();
                        lblName.Visible = true;
                        DropDownList1.Visible = true;
                    }
                }
            }
            catch (Exception)
            {

            }
        }
        public Hashtable FillFullName(IEnumerable<ACC_Voucher> lstVouch)
        {
            Hashtable tblNames = new Hashtable();
            foreach (ACC_Voucher item in lstVouch)
            {
                tblNames.Add(item.Number, item.Number + "_"+item.TE_Student.Ar_FirstName + " " + item.TE_Student.Ar_SecondName + " " + item.TE_Student.Ar_ThirdNmae + " " + item.TE_Student.Ar_FourthName + " ");
            }
            return tblNames;
        }
        protected void gvdata_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            if (e.CommandName == "DoIssue")
            {
                if (!string.IsNullOrEmpty(selectedRow.Cells[1].Text))
                {
                    long tras_Id = 0;
                    long bookid = long.Parse(gvdata.DataKeys[selectedRow.RowIndex]["BookId"].ToString());
                    IEnumerable<SRV_BOOK_Payment_Order> payOrder = School.Service.BOOKS.SRV_Book_PaymentOrder.GetPaymentOrder
                        (long.Parse(selectedRow.Cells[1].Text), bookid);
                    SRV_BOOK_PAYMENT_ITEMS item = payOrder.FirstOrDefault().SRV_BOOK_PAYMENT_ITEMS.Where(s=>s.Book_Id==bookid).FirstOrDefault();
                    if(item!=null)
                        if (db.SRV_Book.Select(s => s).Where(s => s.Id == item.Book_Id).FirstOrDefault().BookQty < item.BookQty)
                    {
                        lblResult.ForeColor = Color.Red;
                        lblResult.Text = "الكميه الموجوده فى المخزن لاتكفى";
                        return;
                    }
                    if (School.Service.BOOKS.SRV_BOOKS_TRANSACTIONS.SRV_Books_TRANSACTIONInsert(new SRV_BOOK_TRANSACTION()
                    {
                        Date_Time = DateTime.Now,
                        Book_Id = item.Book_Id,
                        Qty = item.BookQty,
                        Transaction_Type = 2,
                        Payment_Order_Number = item.Payment_Order_Number
                    }, out tras_Id))
                    {
                        if (tras_Id != 0)
                        {
                            db.SRV_BOOK_TRANSACTION.Where(s => s.Trans_Id == tras_Id).FirstOrDefault().SRV_Book.BookQty =
                                db.SRV_BOOK_TRANSACTION.Where(s => s.Trans_Id == tras_Id).FirstOrDefault().SRV_Book.BookQty - item.BookQty;
                            db.SaveChanges();
                        }
                    }
                    GridBind();
                }
            }
        }
        public void BindGrd()
        {
            lst = School.Service.BOOKS.SRV_BOOK_ISSUE.VoucherSearch(int.Parse(txtNum.Text));
            if (lst != null)
            {
                gvdata.DataSource = lst;
                gvdata.DataBind();

            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex != 0)
            {
                lst = School.Service.BOOKS.SRV_BOOK_ISSUE.VoucherSearch(int.Parse(DropDownList1.SelectedValue));
                if (lst != null)
                {
                    gvdata.DataSource = lst;
                    gvdata.DataBind();
                    txtNum.Text = DropDownList1.SelectedValue;

                }
            }
        }
    }
}